/**
 * 文章发表模块接口列表
 */
import base from './base'; // 导入接口域名列表
import http from '../utils/http'; // 导入http中创建的axios实例
import router from '../router';
import store from '../store';
const setting = {    
    async all(param) {
        try {
            const res = await http.get(`${base.url.core}/setting/list`, {
                params:param
            });
            if (typeof res.data.result != "undefined") {                
                store.commit('SETTING', res.data.result)
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async set(params) {
        try {
            params.value = params.value.toString()
            const res = await http.post(`${base.url.core}/setting/set`,params);
            if (typeof res.data.result != "undefined") {
                //console.log(res.data)
                let setting = JSON.parse(JSON.stringify(store.state.common.setting));                
                setting[params.key] = res.data.result[params.key]              
                store.commit('SETTING', setting)
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
    },
    async setList(params) {
        try {
            params.forEach((v)=>{
                if(typeof params[v.key] != "undefined")params[v.key] = params[v.key].toString()
            })
            const res = await http.post(`${base.url.core}/setting/set`,params);
            if (typeof res.data.result != "undefined") {
                let setting = JSON.parse(JSON.stringify(store.state.common.setting));
                params.forEach((v)=>{
                    setting[v.key] = res.data.result[v.key]
                })
                store.commit('SETTING', setting)
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
    },    
    async getPolicyList(param) {
        try {
            const res = await http.get(`${base.url.core}/policy/list`, {
                params:param
            });
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async addOrUpdatePolicy(params) {
        try {
            const res = await http.post(`${base.url.core}/policy/add_update`,params);
            if (res.data) {
                //console.log(res)                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
    },    
    async delPolicy(params) {
        try {
            let formData = new FormData
            formData.append("id",params.id);            
            const res = await http.post(`${base.url.core}/policy/del`,formData);
            if (res.data) {
                //console.log(res)
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
    },
    async AT(params) {
        try {
            let formData = new FormData
            formData.append("at",params.at);     
            const res = await http.post(`${base.url.core}/logs_at/add`,formData);
            if (res.data) {
                //console.log(res)                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
    },    
    async getAT(param) {
        try {
            const res = await http.get(`${base.url.core}/logs_at/all`, {
                params:param
            });
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },      
    async delAT() {
        try {
            const res = await http.post(`${base.url.core}/logs_at/del`);
            if (res.data) {
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
    },
    async getPinList(param) {
        try {
            const res = await http.get(`${base.url.core}/logs_gpio/list`, {
                params:param
            });
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async getOSSList(params) {
        try {
            const res = await http.post(`${base.url.core}/cloud/list_oss`, params);
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async getTTSList() {
        try {
            const res = await http.get(`${base.url.core}/cloud/list_tts`);
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async getTTYList() {
        try {
            const res = await http.get(`${base.url.core}/device/list_tty`);
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async getVersion() {
        try {
            const res = await http.get(`${base.url.core}/version`);
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    goAbout() {
        router.replace({
            path: '/settings/about',
            query: {
                redirect: router.currentRoute.fullPath
            }
        });
    },
    async doUpdateVersion() {        
        try {
            const res = await http.get(`${base.url.core}/version_update`);
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async getProgress() {
        try {
            const res = await http.get(`${base.url.core}/version/download_progress`);
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
    async doReload() {
        try {
            const res = await http.get(`${base.url.core}/reload`);
            if (res.data) {                
                return res.data.result;
            }
        }
        catch (err) {
            return Promise.reject(err);
        }
        
    },
}
export default setting;
